package com.esirong.whatime.ui.adpater;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.LinkedList;
import java.util.List;

public abstract class AdapterBase<T> extends BaseAdapter {
    protected String tag = this.getClass().getName();
    protected Context mContext;
    protected final List<T> mList = new LinkedList<T>();

    public AdapterBase(Context context, List<T> list) {
        mContext = context;
        appendToTopList(list);
    }

    public List<T> getList() {
        return mList;
    }

    public void appendToList(T item) {
        if (item == null) {
            return;
        }
        mList.add(item);
        notifyDataSetChanged();
    }

    public void appendToTopList(T item) {
        if (item == null) {
            return;
        }
        mList.add(0, item);
        notifyDataSetChanged();
    }

    public void appendToList(List<T> list) {
        if (list == null) {
            return;
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void appendToTopList(List<T> list) {
        if (list == null) {
            return;
        }
        mList.addAll(0, list);
        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public T getItem(int position) {
        if (position > mList.size() - 1) {
            return null;
        }
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == getCount() - 1) {
            onReachBottom();
        }
        return getExView(position, convertView, parent);
    }


    protected abstract View getExView(int position, View convertView, ViewGroup parent);

    protected abstract void onReachBottom();
}
